﻿using System;
using System.Globalization;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.Constants;
using Monkey.Warrenty.EntityModel;
using MonkeyWarranty.Web.Common;

namespace MonkeyWarranty.Web.InstantTickets
{
    public partial class EditEmployee : System.Web.UI.Page
    {
        #region Properties

        private InstantTicket_Employee _ticketEmployee;
        private InstantTicket_Employee TicketEmployee
        {
            get
            {
                if (_ticketEmployee == null)
                {
                    if (TicketEmployeeId > 0)
                    {
                        _ticketEmployee = InstantTicketManager.GetTicketEmployeeById(TicketEmployeeId);
                    }
                }
                return _ticketEmployee;
            }
        }

        protected long TicketId
        {
            get { return (TicketEmployee != null) ? TicketEmployee.InstantTicketId : 0; }
        }

        private Employee _employee;
        protected Employee CurrentEmployee
        {
            get
            {
                if (_employee == null)
                {
                    if (TicketEmployee != null)
                    {
                        _employee = EmployeeManager.GetById(TicketEmployee.EmployeeId);
                    }
                }
                return _employee;
            }
        }

        private long _ticketEmployeeId;
        private long TicketEmployeeId
        {
            get
            {
                if (_ticketEmployeeId <= 0) long.TryParse(Request.Params["id"], out _ticketEmployeeId);
                return _ticketEmployeeId;
            }
        }

        private static EmployeeBiz _employeeBiz;
        private static EmployeeBiz EmployeeManager
        {
            get { return _employeeBiz ?? (_employeeBiz = new EmployeeBiz()); }
        }

        private static InstantTicketBiz _instantTicketBiz;
        private static InstantTicketBiz InstantTicketManager
        {
            get { return _instantTicketBiz ?? (_instantTicketBiz = new InstantTicketBiz()); }
        }

        private static CategoryBiz _categoryBiz;
        private static CategoryBiz CategoryManager
        {
            get { return _categoryBiz ?? (_categoryBiz = new CategoryBiz()); }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (WarrantyCommon.CurrrentEmployee == null)
            {
                Response.Redirect("~/Logon.aspx");
                return;
            }

            if (!WarrantyCommon.CurrrentEmployee.HasRole(RoleConstant.AdminDichVu))
            {
                Response.Redirect("~/NoPermission.aspx");
            }

            if (!IsPostBack)
            {
                if (TicketEmployeeId <= 0 || TicketEmployee == null)
                {
                    lblErrorMessage.Text = Resources.GlobalStrings.EmployeeNotExisted;
                    btnSave.Enabled = false;
                    return;
                }
                LoadData();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;

            // validate inputs
            var employeeId = hidEmployeeId.Value;
            if (string.IsNullOrEmpty(employeeId))
            {
                lblErrorMessage.Text = Resources.GlobalStrings.EmployeeIsRequired;
                return;
            }

            DateTime startDate;
            if (!DateTime.TryParseExact(txtStartDate.Text.Trim(), "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out startDate))
            {
                lblErrorMessage.Text = Resources.GlobalStrings.InvalidDate;
                return;
            }

            var descrition = txtDescription.Text;
            var note = txtNote.Text;

            TicketEmployee.InstantTicketId = TicketId;
            TicketEmployee.EmployeeId = int.Parse(employeeId);
            TicketEmployee.Description = descrition;
            TicketEmployee.Note = note;
            TicketEmployee.StartDate = startDate;

            // call biz update
            InstantTicketManager.UpdateTicketEmployee(TicketEmployee);

            lblErrorMessage.Text = Resources.GlobalStrings.UpdateSuccess;
        }

        #region Helpers

        private void LoadData()
        {
            // ticket employee
            hidEmployeeId.Value = TicketEmployee.EmployeeId.ToString(CultureInfo.InvariantCulture);
            if (CurrentEmployee != null)
            {
                txtEmployeeName.Text = CurrentEmployee.Name;
            }
            txtDescription.Text = TicketEmployee.Description;
            txtNote.Text = TicketEmployee.Note;
            txtStartDate.Text = string.Format("{0:dd/MM/yyyy HH:mm}", TicketEmployee.StartDate);
        }

        #endregion
    }
}